package com.yogee.topseller.fragment.home;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.yogee.core.base.HttpFragment;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.topseller.R;
import com.yogee.topseller.adapter.RankingRvAdapter;
import com.yogee.topseller.bean.PersonalDataBean;
import com.yogee.topseller.bean.RankingBean;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.utils.RefreshUtil;
import com.yogee.topseller.view.RefreshView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @auathor ${XiaoJianghong}
 * @date 2017/7/7.
 */

public class RankingFragment extends HttpFragment implements RefreshUtil.OnRefreshListener {

    @BindView(R.id.ranking_follow_tv)
    TextView followTv;
    @BindView(R.id.ranking_hot_tv)
    TextView hotTv;
    @BindView(R.id.ranking_profit_tv)
    TextView profitTv;
    @BindView(R.id.ranking_yong_tv)
    TextView yongTv;
    @BindView(R.id.ranking_rv)
    RecyclerView rankingRv;
    @BindView(R.id.rank_refreshlayout)
    TwinklingRefreshLayout rankRefreshlayout;
    private RankingRvAdapter rvAdapter;
    Unbinder unbinder;
    private List<RankingBean.ProductListBean> collects =  new ArrayList<>();
    private List<RankingBean.ProductListBean> hots = new ArrayList<>();
    private List<RankingBean.ProductListBean> receipts = new ArrayList<>();
    private List<RankingBean.ProductListBean> commissions = new ArrayList<>();
    private int rankType;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_ranking;
    }

    @Override
    protected void initView() {
        rankRefreshlayout.setOnRefreshListener(new RefreshUtil(this).refreshListenerAdapter());
        rankRefreshlayout.setHeaderView(new RefreshView(getActivity()));
        rankRefreshlayout.setBottomView(new RefreshView(getActivity()));
        rankRefreshlayout.setBottomHeight(50);
        rankingRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        rankingRv.setHasFixedSize(true);
        rvAdapter = new RankingRvAdapter(getActivity());
        rankingRv.setAdapter(rvAdapter);
        collectBoard("0", "10", true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.ranking_follow_tv, R.id.ranking_hot_tv, R.id.ranking_profit_tv, R.id.ranking_yong_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ranking_follow_tv:
                followTv.setTextColor(getResources().getColor(R.color.title_select_color));
                hotTv.setTextColor(Color.parseColor("#FFFFFF"));
                profitTv.setTextColor(Color.parseColor("#FFFFFF"));
                yongTv.setTextColor(Color.parseColor("#FFFFFF"));

                if (collects == null ||collects.size()==0) {
                    collectBoard("0", "10", true);
                } else {
                    rankType = 0;
                    rvAdapter.addData(collects, 0);
                }
                break;
            case R.id.ranking_hot_tv:
                followTv.setTextColor(Color.parseColor("#FFFFFF"));
                hotTv.setTextColor(getResources().getColor(R.color.title_select_color));
                profitTv.setTextColor(Color.parseColor("#FFFFFF"));
                yongTv.setTextColor(Color.parseColor("#FFFFFF"));
                if (hots == null  ||hots.size()==0) {
                    hotBoard("0", "10", true);
                } else {
                    rankType = 1;
                    rvAdapter.addData(hots, 1);
                }
                break;
            case R.id.ranking_profit_tv:
                followTv.setTextColor(Color.parseColor("#FFFFFF"));
                hotTv.setTextColor(Color.parseColor("#FFFFFF"));
                profitTv.setTextColor(getResources().getColor(R.color.title_select_color));
                yongTv.setTextColor(Color.parseColor("#FFFFFF"));
                if (receipts == null||receipts.size()==0) {
                    receiptsBoard("0", "10", true);
                } else {
                    rankType = 2;
                    rvAdapter.addData(receipts, 2);
                }
                break;
            case R.id.ranking_yong_tv:
                followTv.setTextColor(Color.parseColor("#FFFFFF"));
                hotTv.setTextColor(Color.parseColor("#FFFFFF"));
                profitTv.setTextColor(Color.parseColor("#FFFFFF"));
                yongTv.setTextColor(getResources().getColor(R.color.title_select_color));
                if (commissions == null || commissions.size()==0) {
                    commissionBoard("0", "10", true);
                } else {
                    rankType = 3;
                    rvAdapter.addData(commissions, 3);
                }
                break;
        }
    }

    @Override
    public void onRefresh() {
        switch (rankType) {
            case 0:
                collectBoard("0", "10", true);
                break;
            case 1:
                hotBoard("0", "10", true);
                break;
            case 2:
                receiptsBoard("0", "10", true);
                break;
            case 3:
                commissionBoard("0", "10", true);
                break;
        }
    }

    @Override
    public void onLoad() {
        switch (rankType) {
            case 0:
                collectBoard(String.valueOf(collects.size()), "10", false);
                break;
            case 1:
                hotBoard(String.valueOf(hots.size()), "10", false);
                break;
            case 2:
                receiptsBoard(String.valueOf(receipts.size()), "10", false);
                break;
            case 3:
                commissionBoard(String.valueOf(commissions.size()), "10", false);
                break;
        }
    }

    private void collectBoard(String total, String count, final boolean isRefresh) {
        rankType = 0;
        HttpManager.getInstance().collectBoard(total, count).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<RankingBean>() {
            @Override
            public void onNext(RankingBean o) {
                if (isRefresh) {
                    collects = o.getProductList();
                    rankRefreshlayout.finishRefreshing();
                } else {
                    collects.addAll(o.getProductList());
                    rankRefreshlayout.finishLoadmore();
                }
                rvAdapter.addData(collects, rankType);
            }
        }, this));
    }

    private void hotBoard(String total, String count, final boolean isRefresh) {
        rankType = 1;
        HttpManager.getInstance().hotBoard(total, count).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<RankingBean>() {
            @Override
            public void onNext(RankingBean o) {
                if (isRefresh) {
                    hots = o.getProductList();
                    rankRefreshlayout.finishRefreshing();
                } else {
                    hots.addAll(o.getProductList());
                    rankRefreshlayout.finishLoadmore();
                }
                rvAdapter.addData(hots, rankType);
            }
        }, this));
    }

    private void receiptsBoard(String total, String count, final boolean isRefresh) {
        rankType = 2;
        HttpManager.getInstance().receiptsBoard(total, count).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<RankingBean>() {
            @Override
            public void onNext(RankingBean o) {
                if (isRefresh) {
                    receipts = o.getProductList();
                    rankRefreshlayout.finishRefreshing();
                } else {
                    receipts.addAll(o.getProductList());
                    rankRefreshlayout.finishLoadmore();
                }
                rvAdapter.addData(receipts, rankType);
            }
        }, this));
    }

    private void commissionBoard(String total, String count, final boolean isRefresh) {
        rankType = 3;
        HttpManager.getInstance().commissionBoard(total, count).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<RankingBean>() {
            @Override
            public void onNext(RankingBean o) {
                if (isRefresh) {
                    commissions = o.getProductList();
                    rankRefreshlayout.finishRefreshing();
                } else {
                    commissions.addAll(o.getProductList());
                    rankRefreshlayout.finishLoadmore();
                }
                rvAdapter.addData(commissions, rankType);
            }
        }, this));
    }
}
